package com.hyyt.shopjsp.customer.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.hyyt.shopjsp.finance.service.IShopFundDetailService;
import com.hyyt.shopjsp.finance.service.IShopMallCoinService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hyyt.shopjsp.basic.pojo.BasicCustomer;
import com.hyyt.shopjsp.basic.service.IBasicCustomerService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.customer.pojo.ShopCustomerBankroll;
import com.hyyt.shopjsp.customer.pojo.ShopCustomerInfo;
import com.hyyt.shopjsp.customer.pojo.ShopCustomerInvoice;
import com.hyyt.shopjsp.customer.service.IShopCustomerBankrollService;
import com.hyyt.shopjsp.customer.service.IShopCustomerInfoService;
import com.hyyt.shopjsp.customer.service.IShopCustomerInvoiceService;
import com.hyyt.shopjsp.finance.pojo.ShopRecharge;
import com.hyyt.shopjsp.finance.service.IShopRechargeService;
import com.hyyt.shopjsp.order.service.IShopOrdersService;
import com.hyyt.shopjsp.store.info.service.IShopShopInfoService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import com.hyyt.shopjsp.wxmg.fans.pojo.WxFansUserInfo;
import com.hyyt.shopjsp.wxmg.fans.service.IWxFansUserInfoService;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/3/31 0024上午 11:20.
 */
@Controller
@RequestMapping("/back/customer")
public class ShopCustomerController extends BaseController {
	private Logger log = Logger.getLogger(ShopCustomerController.class);
	private String basePath = "/back/shop/customer/";

	@Resource
	private IBasicCustomerService basicCustomerService;//会员service
	@Resource
	private IShopCustomerInfoService shopCustomerInfoService;//会员信息service
	@Resource
	private IShopFundDetailService shopFundDetailService;//资金流水service
    @Resource
	private IShopMallCoinService shopMallCoinService;//积分明细service
	@Resource
	private IShopShopInfoService shopShopInfoService;//店铺service
	@Resource
	private IWxFansUserInfoService wxFansUserInfoService;//粉丝用户信息service
	/**
	 * 客户发票信息Service
	 */
	@Resource
	private IShopCustomerInvoiceService shopCustomerInvoiceService;//用户发票service
	@Resource
	private IShopRechargeService shopRechargeService;//充值记录service
	@Resource
	private IShopCustomerBankrollService shopCustomerBankrollService;//账户资金记录service
	@Resource
	private IShopOrdersService shopOrdersService;//订单service
	/**
	 * 进入会员信息管理
     *
	 * @return 返回到指定页面
	 */
	@RequestMapping("gotoCustomerPage")
	public String gotoCustomerPage(){
		return basePath+"customerList";
	}

	/**
	 * 查询所有会员信息
	 * @param loginName 会员名称
	 * @param phone  手机号
	 * @param email  邮箱
	 * @param lockedState  冻结状态
	 * @param wxUserOpenId  微信用户开放Id
	 * @param page  分页
	 * @param response 返回结果集
	 */
	@RequestMapping(value="listCustomer",method=RequestMethod.POST)
	@ResponseBody
	public void listCustomer(String loginName,String phone,String email,String lockedState,String wxUserOpenId,Page page, HttpServletResponse response){
		Map<String, Object> param = new HashMap<>();
		if(Utils.objectIsNotEmpty(loginName)){//是否检索会员名称
			param.put("loginName", loginName.trim());
		}
		if(Utils.objectIsNotEmpty(phone)){//是否检索手机号
			param.put("phone", phone.trim());
		}
		if(Utils.objectIsNotEmpty(email)){//是否检索邮箱
			param.put("email", email.trim());
		}
		if(Utils.objectIsNotEmpty(lockedState)){//是否检索状态
			param.put("lockedState", lockedState);
		}
		if("1".equals(wxUserOpenId)){
			param.put("wxUserOpenId", "is not null");
		}
		if("0".equals(wxUserOpenId)){
			param.put("wxUserOpenId", "is null");
		}
		param.put("sortColumns", "customerId desc");
		int totalRecordCount =basicCustomerService.countBasicCustomer(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<Map<String, Object>> customerList =basicCustomerService.queryBasicCustomerByCustomerId(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", customerList);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
     * 根据会员信息id获得对象
     *
     * @param customerId 会员Id
     */
	@RequestMapping(value = "getCustomerObject", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg getCustomerObject(Integer customerId) {
        Map<String, Object> params = new HashMap<>();
        params.put("customerId",customerId);
		if (Utils.objectIsNotEmpty(customerId)) {
            List<Map<String, Object>> list = basicCustomerService.queryCollectionByCustomerId(params);
            BigDecimal countBankroll = shopFundDetailService.updateOrGetCustomerBankroll(customerId, "");
            BigDecimal countCoin = shopMallCoinService.updateOrGetMallCoinBalanceById(customerId,"");
            params.clear();
            params.put("list",list);
            params.put("countBankroll",countBankroll);
            params.put("countCoin",countCoin);
            return ResultMsg.create(true,null,FastJsonUtils.toJSONString(params));
		}else {
            return ResultMsg.create(false);
        }
	}

	/**
	 * 修改用户冻结状态
	 * @param customerId 会员Id
	 * @param lockedState 冻结状态
	 * @return
	 */
	@RequestMapping(value="changeLockedState",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg changeLockedState(Integer customerId,String lockedState){
		Map<String,Object> param = new HashMap<>();
		if (Utils.objectIsNotEmpty(customerId)&&Utils.objectIsNotEmpty(lockedState)){
			param.put("customerId",customerId);
			param.put("lockedState",lockedState);
			basicCustomerService.updateBasicCustomerByCustomerId(param);
			return ResultMsg.create(true);
		}else {
			return ResultMsg.create(false);
		}
	}
	
	/**
	 * 会员充值金额
	 * @param customerId 会员Id
	 * @param rechargeAmount 充值金额
	 * @return
	 */
	@RequestMapping(value="gotoRechargeMoney",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg gotoRechargeMoney(String customerId,BigDecimal rechargeAmount){
		boolean flag=false;
		if (Utils.objectIsNotEmpty(customerId)&&Utils.objectIsNotEmpty(rechargeAmount)){
            shopRechargeService.saveRechargeMoney(customerId,rechargeAmount);
            flag=true;
            return ResultMsg.create(flag);
        }else{
            return ResultMsg.create(flag);
        }
	}

	/**
	 * 前往会员充值列表
	 *
	 * @return 返回到指定页面
	 */
	@RequestMapping("gotoRechargePage")
	public String gotoRechargePage(String customerId, Map<String, Object> param){
		BasicCustomer customer=basicCustomerService.loadBasicCustomer(Integer.parseInt(customerId));
		ShopCustomerBankroll customerBankroll=shopCustomerBankrollService.loadShopCustomerBankroll(Integer.parseInt(customerId));
		param.put("customerBankroll", customerBankroll);
		if(Utils.objectIsNotEmpty(customer)){
			param.put("customerName", customer.getLoginName());
		}
		param.put("customerId", customerId);
		return basePath+"rechargeList";
	}

	/**
	 * 充值明细列表
	 * @param customerId 会员Id
	 * @param page 分页
	 * @param response 返回结果集
	 */
	@RequestMapping(value="customerRechargeList",method=RequestMethod.POST)
	public void customerRechargeList(String customerId,String rechargeCode,String state,Page page, HttpServletResponse response){
		Map<String, Object> param=new HashMap<>();
		if(Utils.objectIsNotEmpty(rechargeCode)){//是否检索邮箱
			param.put("rechargeCode", rechargeCode.trim());
		}
		if(Utils.objectIsNotEmpty(state)){//是否检索状态
			param.put("state", state);
		}
		param.put("customerId", customerId);
		int totalRecordCount =shopRechargeService.countShopRecharge(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopRecharge> rechargeList =shopRechargeService.queryShopRecharge(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", rechargeList);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
	 * 前往会员金币列表
     *
	 * @return 返回到指定页面
	 */
	@RequestMapping("gotoCustomerMallCoinPage")
	public String gotoCustomerMallCoinPage(String customerId, Map<String, Object> param){
		param.put("customerId", customerId);
		return basePath+"customerMallCoinList";
	}

	/**
	 * 会员发票信息
	 * @param customerId 会员Id
	 * @return 返回结果集
	 */
	@RequestMapping(value="findInvoiceInfo",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg findInvoiceInfo(String customerId){
		if(Utils.objectIsNotEmpty(customerId)){
			ShopCustomerInvoice customerInvoice = shopCustomerInvoiceService.loadShopCustomerInvoice(Integer.parseInt(customerId));
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(customerInvoice));
		}else {
			return ResultMsg.create(false);
		}
	}

	/**
	 * 会员微信粉丝信息
	 * @param customerId 会员Id
	 * @return 返回结果集
	 */
	@RequestMapping(value="getFansUserInfoObject",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg getFansUserInfoObject(String customerId){
		if(Utils.objectIsNotEmpty(customerId)){
			Map<String, Object> param=new HashMap<>();
			ShopCustomerInfo shopCustomerInfo = shopCustomerInfoService.loadShopCustomerInfo(Integer.parseInt(customerId));
			if(shopCustomerInfo!=null&&shopCustomerInfo.getWxUserOpenId()!=null&&shopCustomerInfo.getWxUserOpenId()!=""){
				param.put("userOpenId",shopCustomerInfo.getWxUserOpenId());
				WxFansUserInfo fansUserInfo = wxFansUserInfoService.loadWxFansUserInfoWithUserOpenId(param);
				return ResultMsg.create(true, null, FastJsonUtils.toJSONString(fansUserInfo));
			}else{
				return ResultMsg.create(false);
			}
		}else{
			return ResultMsg.create(false);
		}
	}

}